<template>
  <div class="login-container">
    <div class="loginBox">
      <div class="left"></div>
      <div class="right">
        <div class="title-container">欢迎登录！</div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">

          <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              auto-complete="on"
              name="username"
              placeholder="请输入账号"
              tabindex="1"
              type="text"
            />
          </el-form-item>

          <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              auto-complete="on"
              name="password"
              placeholder="请输入密码"
              tabindex="2"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
        </span>
          </el-form-item>

          <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
        <div class="loginChe">
          <div>
            <input id="psd" type="checkbox"></input>
            <label for="psd">记住密码</label>
          </div>

          <div>
            <input id="psd" type="checkbox"></input>
            <label for="psd">自动登录</label>
          </div>

        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-image: url("../../assets/bg1.png");
  background-repeat: no-repeat;
  background-size: cover;
}

.loginBox {
  width: 900px;
  height: 500px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
  display: flex;

  .left {
    height: 100%;
    flex: 1.7;
    background: url("../../assets/bg2.png") no-repeat;
    background-size: cover;
  }

  .right {
    height: 100%;
    flex: 1;
    background: #fff;
    padding: 50px 30px;
    box-sizing: border-box;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;

    .el-form {
      width: 100%;
      margin-top: 50px;

      .el-form-item /deep/ .el-form-item__content {
        display: flex;
        color: #9b999c;
        border-bottom: 1px solid #9b999c;

        input {
          border: none;
          color: #9b999c;
          font-family: puhuiti-2-65;
          &::placeholder{
            color:#9b999c;
            font-family: puhuiti-2-65;
          }
        }
      }

      button {
        background: #33394b;
        width: 100%;
        margin-top:50px;
        font-family: puhuiti-2-65;
      }
    }

    .title-container {
      text-align: center;
      color: #295a8a;
      font-size: 24px;
      letter-spacing: 2px;
      color:#1f5285;
    }
  }
}
.loginChe{
  width:100%;
  display: flex;
  justify-content: space-between;
  margin-top:20px;
  label{
    margin-left:5px;
    font-family: puhuiti-2-65;
    font-weight: 400 !important;
    color:#9c9a9d;
  }
  #psd{
    padding:2px;
  }
}
</style>
